package com.zmn.plat.admin.controller.channel.price.faultprice;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.plat.admin.controller.channel.ChannelBaseController;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.ChannelFaultPriceVO;
import com.zmn.plat.model.entity.service.tariff.ServiceTariff;

/**
 * 查询故障价格表
 *
 * @author xiangjunming
 * @since 2019/12/04 17:51:52
 */
@Controller
@RequestMapping("/channel/price/faultprice")
public class ChannelFaultPriceListController extends ChannelBaseController {

    /**
     * 渠道故障价格表-列表
     *
     * @param channelId
     *            the channel id
     * @param addFlag
     *            the add flag
     * @return model and view
     */
    @RequestMapping(value = "list")
    public ModelAndView list(@RequestParam(value = "channelId", defaultValue = "0") Integer channelId, String addFlag) {

        ModelAndView mav = new ModelAndView("channel/price/faultprice/list");

        Channel obj = channelService.findByKey(channelId);
        List<ChannelFaultPriceVO> faultPrices = channelTariffService.listByChannelId(channelId);
        Optional<ChannelFaultPriceVO> faultPriceOut = faultPrices.stream().filter(item -> item.getBizType() == 1).findFirst();
        Optional<ChannelFaultPriceVO> faultPriceIn = faultPrices.stream().filter(item -> item.getBizType() == 2).findFirst();

        List<ChannelFaultPriceVO> items = new ArrayList<>();
        items.add(faultPriceOut.orElseGet(() -> new ChannelFaultPriceVO() {
            {
                setChannelId(channelId);
                setBizType(1);
            }
        }));
        items.add(faultPriceIn.orElseGet(() -> new ChannelFaultPriceVO() {
            {
                setChannelId(channelId);
                setBizType(2);
            }
        }));

        mav.addObject("obj", obj);
        mav.addObject("flag", 5);
        mav.addObject("addFlag", addFlag);
        mav.addObject("channelId", channelId);
        mav.addObject("items", items);

        return mav;
    }

    /**
     * to故障价格表选择页面
     *
     * @param channelId
     *            the channel id
     * @param bizType
     *            the keep type
     * @param tariffType
     * @return model and view
     */
    @RequestMapping(value = "select")
    public ModelAndView select(Integer channelId, Integer bizType, Integer tariffType) {
        ModelAndView mav = new ModelAndView("channel/price/faultprice/select");
        List<ServiceTariff> faultTariffs = faultTariffService.listAll();
        List<ServiceTariff> items = faultTariffs.stream().filter(item -> Objects.equals(bizType, item.getBizType()))
            .filter(item -> Objects.equals(tariffType, item.getTariffType()))
            .filter(item -> Objects.equals(GlobalConsts.YES, item.getStatus())).collect(Collectors.toList());
        mav.addObject("channelId", channelId);
        mav.addObject("bizType", bizType);
        mav.addObject("tariffType", tariffType);
        mav.addObject("items", items);
        return mav;
    }
}
